package com.qingpeng.pay.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qingpeng.pay.R;
import com.qingpeng.pay.adapter.QRCodeMoneyListAdapter;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.QRCodeMoneyListBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class QRcodeListActivity extends BaseActivity {

    @BindView(R.id.title)
    TitleBar title;
    private String mType;
    private List<QRCodeMoneyListBean.DataProductBean> mData;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_qrcode_list;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.recycler;
    }

    @Override
    protected void onResume() {
        super.onResume();
        title.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mType = getIntent().getStringExtra("type");
        Call<QRCodeMoneyListBean> qrCodeListBeanCall = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).erweima_list();
        qrCodeListBeanCall.enqueue(new Callback<QRCodeMoneyListBean>() {
            @Override
            public void onResponse(Call<QRCodeMoneyListBean> call, Response<QRCodeMoneyListBean> response) {
                QRCodeMoneyListBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(QRcodeListActivity.this, body.getInfo());
                    return;
                }
                mData = body.getData();
                MyinitAdapter();
            }

            @Override
            public void onFailure(Call<QRCodeMoneyListBean> call, Throwable t) {

            }
        });
    }

    protected void MyinitAdapter() {
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        mListView.setLayoutManager(layoutManager);
        mAdapter = new QRCodeMoneyListAdapter(R.layout.item_moneylist);
        mAdapter.setNewData(mData);
        mListView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(QRcodeListActivity.this, UpQRCodeListActivity.class)
                        .putExtra("type", mType)
                        .putExtra("money", mData.get(position).getMoney()));
            }
        });
    }
}
